package com.robotserver.orders.web;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.constant.ResponseCode;
import com.robotserver.orders.constant.ResponseMsg;
import com.robotserver.orders.entity.TbOrders;
import com.robotserver.orders.exception.BusinessException;
import com.robotserver.orders.mapper.TbOrdersMapper;
import com.robotserver.orders.param.TbOrderSyncParam;
import com.robotserver.orders.param.TbOrderSyncParamV3;
import com.robotserver.orders.param.biz.TbOrderPullParam;
import com.robotserver.orders.service.TbOrderManualService;
import com.robotserver.orders.service.TbOrderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: jianghan
 * @Date: 2019/11/14 09:42
 * @Description:
 */
@RestController
public class TbOrderController {

    private static Logger logger = LoggerFactory.getLogger(TbOrderController.class);

    @Autowired
    private TbOrderManualService tbOrderManualService;
    @Autowired
    private TbOrderService tbOrderService;
    @Autowired
    private TbOrdersMapper tbOrdersMapper;

    /**
     * 本地下载
     * @param param
     * @return
     */
    @PostMapping("tborder/pull")
    public Object pullOrder(TbOrderPullParam param){
        return tbOrderService.getOrderList(param);
    }

    /**
     * 本地下载(新版临时添加)
     * @param param
     * @return
     */
    @PostMapping("tborder/pullNew")
    public Object pullOrderNew(TbOrderPullParam param){
        return tbOrderService.getOrderListNew(param);
    }

    /**
     * 远程下载
     * @param tbOrderSyncParam
     * @return
     */
    @PostMapping("tborder/remotePull")
    public Object remotePull(TbOrderSyncParam tbOrderSyncParam){

        logger.info("--2.0手动远程下载淘宝订单--tbOrderRefundSyncParam={}", JSONObject.toJSONString(tbOrderSyncParam));

        if (tbOrderSyncParam.getUserId()==null || StringUtils.isBlank(tbOrderSyncParam.getUserName())
                || StringUtils.isBlank(tbOrderSyncParam.getAccessToken())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(tbOrderSyncParam.getBegin_time() < (Long.valueOf(now)-7776000)){
            logger.error("------远程单个token错误，开始时间不能小于90天前。-------"+tbOrderSyncParam.getAccessToken()+"::"+tbOrderSyncParam.getUserName()+"::"+tbOrderSyncParam.getBegin_time()+"::"+tbOrderSyncParam.getEnd_time());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        tbOrderManualService.remotePull(tbOrderSyncParam);
        return true;
    }

    /**
     * 远程下载所有
     * @param tbOrderSyncParam
     * @return
     */
    @PostMapping("tborder/remote15Pull")
    public Object remote15Pull(TbOrderSyncParam tbOrderSyncParam){

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(tbOrderSyncParam.getBegin_time() < (Long.valueOf(now)-7776000)){
            logger.error("------远程单个token错误，开始时间不能小于90天前。-------"+tbOrderSyncParam.getAccessToken()+"::"+tbOrderSyncParam.getUserName()+"::"+tbOrderSyncParam.getBegin_time()+"::"+tbOrderSyncParam.getEnd_time());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        tbOrderService.syncOrder(tbOrderSyncParam);
        return true;
    }

    /**
     * 根据订单号获取订单
     * @param orderId
     * @return
     */
    @PostMapping("tborder/getByOrderId")
    public Object getByOrderId(String orderId){
        return tbOrderService.getByOrderId(orderId);
    }


    /**
     * 根据父子单号获取订单
     * @return
     */
    @PostMapping("tborder/getByParAndCliId")
    public Object getByParAndCliId(String parent_order_no,String child_order_no,Long createdt,String accessToken,String alimmUserId,String alimmUserName){
        TbOrders tbOrders = tbOrderService.getByParAndCliId(parent_order_no,child_order_no,createdt,accessToken,alimmUserId,alimmUserName);
        return tbOrders;
    }

    /**
     * 远程下载-3.0V
     * @param tbOrderSyncParamV3
     * @return
     */
    @PostMapping("tborder/remotePullV3")
    public Object remotePullV3(TbOrderSyncParamV3 tbOrderSyncParamV3){

        logger.info("--3.0手动远程下载淘宝订单--tbOrderRefundSyncParam={}", JSONObject.toJSONString(tbOrderSyncParamV3));

        if (tbOrderSyncParamV3.getUserId() == null
                || StringUtils.isBlank(tbOrderSyncParamV3.getUserName())
                || StringUtils.isBlank(tbOrderSyncParamV3.getAccessToken())
                || tbOrderSyncParamV3.getBeginTime() == null
                || tbOrderSyncParamV3.getEndTime() == null
                || tbOrderSyncParamV3.getOrderScene() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(tbOrderSyncParamV3.getBeginTime() < (Long.valueOf(now)-7776000)){
            logger.error("------远程拉取淘宝订单V3版本错误，开始时间不能小于90天前。-------" + tbOrderSyncParamV3.getAccessToken() + "::" + tbOrderSyncParamV3.getBeginTime() + "::" + tbOrderSyncParamV3.getEndTime());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        tbOrderManualService.remotePullV3(tbOrderSyncParamV3);
        return true;
    }

    /**
     * 远程补拉全部联盟订单-3.0V
     * @param tbOrderSyncParamV3
     * @return
     */
    @PostMapping("tborder/pullOrderByAllToken")
    public Object pullOrderByAllToken(TbOrderSyncParamV3 tbOrderSyncParamV3){

        logger.info("--god平台手动远程下载淘宝订单--tbOrderRefundSyncParam={}", JSONObject.toJSONString(tbOrderSyncParamV3));

        if (tbOrderSyncParamV3.getBeginTime() == null
                || tbOrderSyncParamV3.getEndTime() == null
                || tbOrderSyncParamV3.getOrderScene() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(tbOrderSyncParamV3.getBeginTime() < (Long.valueOf(now)-7776000)){
            logger.error("------god平台手动远程下载淘宝订单，开始时间不能小于90天前。-------" + tbOrderSyncParamV3.getBeginTime() + "::" + tbOrderSyncParamV3.getEndTime());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        //1:常规订单，2:渠道订单，3:会员运营订单，默认为1
        if(tbOrderSyncParamV3.getOrderScene() == null || tbOrderSyncParamV3.getOrderScene().longValue() == 1){
            //普通订单
            TbOrderSyncParam tbOrderSyncParam = new TbOrderSyncParam();
            tbOrderSyncParam.setBegin_time(tbOrderSyncParamV3.getBeginTime());
            tbOrderSyncParam.setEnd_time(tbOrderSyncParamV3.getEndTime());
            tbOrderSyncParam.setTimeType(tbOrderSyncParamV3.getQueryType()); // 1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询 4:按照订单更新时间
            tbOrderSyncParam.setRedisQueueKey(CacheKey.tbOrderQueue);
            //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
            tbOrderService.syncOrder_producer(tbOrderSyncParam);
        }else if(tbOrderSyncParamV3.getOrderScene().longValue() == 2){
            //渠道订单
            TbOrderSyncParam tbOrderSyncParamChannel = new TbOrderSyncParam();
            tbOrderSyncParamChannel.setBegin_time(tbOrderSyncParamV3.getBeginTime());
            tbOrderSyncParamChannel.setEnd_time(tbOrderSyncParamV3.getEndTime());
            tbOrderSyncParamChannel.setTimeType(tbOrderSyncParamV3.getQueryType()); // 1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询 4:按照订单更新时间
            tbOrderSyncParamChannel.setOrderScene(2L); //渠道
            tbOrderSyncParamChannel.setRedisQueueKey(CacheKey.tbOrderChannelQueue);
            //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
            tbOrderService.syncOrder_producer(tbOrderSyncParamChannel);
        }else if(tbOrderSyncParamV3.getOrderScene().longValue() == 3){ //会员订单
            TbOrderSyncParam tbOrderSyncParamSld = new TbOrderSyncParam();
            tbOrderSyncParamSld.setBegin_time(tbOrderSyncParamV3.getBeginTime());
            tbOrderSyncParamSld.setEnd_time(tbOrderSyncParamV3.getEndTime());
            tbOrderSyncParamSld.setTimeType(tbOrderSyncParamV3.getQueryType()); // 1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询 4:按照订单更新时间
            tbOrderSyncParamSld.setOrderScene(3l); //会员
            tbOrderSyncParamSld.setRedisQueueKey(CacheKey.tbOrderSldQueue);
            //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
            tbOrderService.syncOrder_producer(tbOrderSyncParamSld);
        }

        return true;
    }
}
